package com.carepad.util;

import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.carepad.entity.Users;
import com.carepad.exception.NullReferenceAccessException;

public class SessionVariableManage implements SessionAware {

	private static Logger logger = Logger.getLogger(SessionVariableManage.class);
	private static final String SESSION_NOT_EXIST = "Session doesn't exist";
	private Map<SessionKey, Object> session = null;

	public void addUserVariable(Users user) throws NullReferenceAccessException {
		if(session != null) {
			session.put(SessionKey.SESSION_USER, user);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new NullReferenceAccessException(SESSION_NOT_EXIST);
		}
	}

	public void removeUserVariable(Users user) throws NullReferenceAccessException {
		if(session != null) {
			session.remove(SessionKey.SESSION_USER);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new NullReferenceAccessException(SESSION_NOT_EXIST);
		}
	}

	public boolean verifySessionUser() throws NullReferenceAccessException {
		if(session != null) {
			return session.containsKey(SessionKey.SESSION_USER);
		} else {
			logger.error(SESSION_NOT_EXIST);
			throw new NullReferenceAccessException(SESSION_NOT_EXIST);
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public void setSession(@SuppressWarnings("rawtypes")Map session) {
		this.session = session;
	}

}
